﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Interfaces;
using System.ComponentModel.Design;
using OpenComposite.EII.Designer.Workflow.Forms;
using System.IO;
using OpenComposite.EII.CodeGenerators;
using OpenComposite.EII.Services;
using OpenComposite.Forms;

namespace OpenComposite.EII.Designer.SCA
{
	public partial class UCGenerateService : UserControl, IRepositoryController
	{
		public UCGenerateService()
		{
			_svcmodule = null;
			_verbs = null;
			_isLoaded = false;

			InitializeComponent();
		}

		#region IRepositoryController Members

		public System.ComponentModel.Design.DesignerVerbCollection Verbs
		{
			get
			{
				return _verbs;
			}
			set
			{
				_verbs = value;
			}
		}

		public OpenComposite.EII.Repository.IRepositoryItem RepositoryItem
		{
			get { return _svcmodule; }
		}

		public string Title
		{
			get { return "Generate Web Service"; }
		}
		public Image Image
		{
			get { return null; }
		}
		public bool IsLoaded
		{
			get { return _isLoaded; }
		}

		public object GetView()
		{
			return this;
		}

		public void Initialize(OpenComposite.EII.Repository.IRepositoryItem repositoryItem)
		{
			_svcmodule = repositoryItem as Repository.ServiceModule;
			_verbs = null;

			if ( _svcmodule != null ) {
				_svcmodule.ExtendedProperties.BuildType = OpenComposite.EII.Repository.WorkflowBuildType.WCF_IIS;
				ServiceContainer sc = _svcmodule.GetService(typeof(ServiceContainer)) as ServiceContainer;
				if ( sc != null ) {
					ITaskListService tl = _svcmodule.GetService(typeof(ITaskListService)) as ITaskListService;
					if ( tl == null ) {
						sc.AddService(typeof(ITaskListService), new TaskErrorListService(_svcmodule));
					}
				}
				ITaskListService tasklist = _svcmodule.GetService(typeof(ITaskListService)) as ITaskListService;
				if ( tasklist != null ) {
					Control c = tasklist.GetView(_svcmodule) as Control;
					c.Dock = DockStyle.Fill;
					groupTasksErrors.Controls.Add(c);
				}
			}
		}

		public void BeginLoad()
		{
			txtTargetFolder.Text = _svcmodule.ExtendedProperties.DeployPath;
			_isLoaded = true;

			OnLoaded();
		}

		public void Flush()
		{
		}

		public void Save()
		{
		}

		public void BeginUnload()
		{
		}

		public event EventHandler Loaded;

		public event EventHandler Saved;

		public event EventHandler Flushed;

		public event CancelEventHandler Unloading;

		public event EventHandler Unloaded;

		public event EventHandler Activated;

		public event EventHandler Deactivated;

		#endregion

		#region Protected Members

		#region Methods
		protected void OnLoaded()
		{
			EventHandler tmp = this.Loaded;
			if ( tmp != null ) {
				tmp(this, EventArgs.Empty);
			}
		}
		protected void OnSaved()
		{
			EventHandler tmp = this.Saved;
			if ( tmp != null ) {
				tmp(this, EventArgs.Empty);
			}
		}
		protected void OnFlushed()
		{
			EventHandler tmp = this.Flushed;
			if ( tmp != null ) {
				tmp(this, EventArgs.Empty);
			}
		}
		protected void OnUnloading(CancelEventArgs e)
		{
			CancelEventHandler tmp = this.Unloading;
			if ( tmp != null ) {
				tmp(this, e);
			}
		}
		protected void OnUnloaded()
		{
			EventHandler tmp = this.Unloaded;
			if ( tmp != null ) {
				tmp(this, EventArgs.Empty);
			}
		}
		protected void OnActivated()
		{
			EventHandler tmp = this.Activated;
			if ( tmp != null ) {
				tmp(this, EventArgs.Empty);
			}
		}
		protected void OnDeactivated()
		{
			EventHandler tmp = this.Deactivated;
			if ( tmp != null ) {
				tmp(this, EventArgs.Empty);
			}
		}
		#endregion

		#endregion

		#region Private Members

		#region Fields
		private bool _isLoaded;
		private Repository.ServiceModule _svcmodule;
		private System.ComponentModel.Design.DesignerVerbCollection _verbs;
		#endregion

		#region Methods
		private bool selectTargetPath()
		{
			string path = txtTargetFolder.Text;
			if ( !string.IsNullOrEmpty(path) && Directory.Exists(path) ) {
				dlgSelectFolder.SelectedPath = path;
				if ( !dlgSelectFolder.SelectedPath.EndsWith("\\") )
					dlgSelectFolder.SelectedPath += "\\";
			}
			if ( dlgSelectFolder.ShowDialog() == DialogResult.OK ) {
				txtTargetFolder.Text = dlgSelectFolder.SelectedPath;
				_svcmodule.ExtendedProperties.DeployPath = txtTargetFolder.Text;
				return true;
			} else {
				return false;
			}
		}
		private void deployComposite(bool update)
		{
			try {
				if ( string.IsNullOrEmpty(txtTargetFolder.Text) || !Directory.Exists(txtTargetFolder.Text) ) {
					if ( !selectTargetPath() ) return;
				}
				CompositeDeployment deployment = new CompositeDeployment(_svcmodule);
				deployment.DeployComposite(_svcmodule, txtTargetFolder.Text, update, rbnStub.Checked);
			} catch ( Exception ex ) {
				MessageBox.Show(ex.Message, btnUpdate.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		#endregion

		#region Event Handlers
		private void btnSelectTargetFolder_Click(object sender, EventArgs e)
		{
			selectTargetPath();
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			deployComposite(true);
		}

		private void btnRegenerate_Click(object sender, EventArgs e)
		{
			deployComposite(false);
		}
		#endregion

		#endregion

	}
}
